package com.cnsoft.portal.login;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cnsoft.framework.beans.MyBeanFactoryHelper;
import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.beans.user.LoginerBean;
import org.cnsoft.framework.beans.user.UserBean;
import org.cnsoft.framework.security.PasswordHelper;
import org.cnsoft.framework.support.MyServiceSupport;
import org.cnsoft.framework.token.TokenServiceImpl;
import org.cnsoft.framework.utils.DateHelper;
import org.cnsoft.framework.utils.EmptyHelper;
import org.cnsoft.framework.utils.PKHelper;
import org.cnsoft.persistent.common.SystemManager.SystemManagerDBO;
import org.cnsoft.persistent.common.SystemManager.SystemManagerDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

/**
 * 超管登录
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 * @see ApiManagerLoginController
 */
@Service("ISManagerLoginService") // 替换系统登录接口
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ManagerLoginService extends MyServiceSupport implements ISManagerLoginService 
{

	public RESTResultBean<UserBean> doLogin(HttpServletRequest request, HttpServletResponse response, LoginerBean loginer) throws Exception {
		logger.debug("UserBean==>>>" + loginer);
		RESTResultBean<UserBean> result = new RESTResultBean<UserBean>();

		// 参数校验
		if (EmptyHelper.isEmpty(loginer.getAccount())) {
			result.setCode(1);
			result.setMsg("登录账户不能为空");
		} else if (EmptyHelper.isEmpty(loginer.getPassWord())) {
			result.setCode(1);
			result.setMsg("密码不能为空");
		} else {
			SystemManagerDao _SystemManagerService_ = MyBeanFactoryHelper.getBean(SystemManagerDao.class);
			SystemManagerDBO _SystemManagerDBO_ = new SystemManagerDBO();
			// s9010UserDBO.setAccountType(accounttype);//1用户名2手机3邮箱
			_SystemManagerDBO_.setAccount(loginer.getAccount());
			// _SystemManagerDBO_.setHdp(loginer.getHdp());//区分用户渠道类别

			List<SystemManagerDBO> users = _SystemManagerService_.doSelectManager(_SystemManagerDBO_);
			if (users == null) {
				result.setCode(1);
				result.setMsg("用户不存在");
			} else if (users.size() == 0) {
				result.setCode(1);
				result.setMsg("用户不存在");
			} else if (users.size() > 1) {
				result.setCode(2);
				result.setMsg("账户异常");
			} else {
				_SystemManagerDBO_ = users.get(0);// 获得 用户数据

				if (_SystemManagerDBO_.getPassWord().equals(PasswordHelper.getPassword(loginer.getPassWord()))) {
					_SystemManagerDBO_.setPassWord(null);// 保证安全
					UserBean curUser = new UserBean(_SystemManagerDBO_.getId());
					BeanUtils.copyProperties(_SystemManagerDBO_, curUser);

					// 保存系统类别
					curUser.setSid(_SystemManagerDBO_.getSid());
					curUser.setIsAdmin(ONE);////////////////////////////////////////////////////////

					// 缓存中心缓存用户对象
					// 制作Token
					TokenServiceImpl tokenService = TokenServiceImpl.build(request, curUser);
					tokenService.setCacheService(myCacheService);
					tokenService.prepareClientTimestamp();

					tokenService.setWaitTimeSecond(1800);
					tokenService.saveToken(request);// 处理TOKEN

					// 记录登录信息
					{
						_SystemManagerDBO_.setId(PKHelper.creatPUKey());
						_SystemManagerDBO_.setLoginStatus(ZERO);
						_SystemManagerDBO_.setOnlineType(TWO);
						// _SystemManagerDBO_.setOnlineLastIp(tokenBean.getIpAdress());
						_SystemManagerDBO_.setOnlineLastTime(DateHelper.currentTimeMillisCN1());
						_SystemManagerService_.saveLoginLog(_SystemManagerDBO_);
					}

					result.setData(tokenService);

				} else {
					result.setCode(3);
					result.setMsg("密码错误");
				}
			}
		}
		logger.debug("RESTResultBean==>>>" + result.toJsonString());
		return result;
	}
}
